/*
* 1videoConference -- An open source video conferencing platform.
*
* Copyright (C) 2007 - 2008, Adiance Technologies Pvt. Ltd.
*
* Hardik Sanghvi <hardik.sanghvi @adiance.com>
*
* See http://www.1videoconference.org for more information about
* the 1videoConference project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, forums and mailing lists
* for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*/

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ServiceModel;
using System.Globalization;
using System.Security;
using System.Security.Permissions;
using _videoConference.BAL;
using System.Deployment.Application;
using ZipInfo;
using System.Text.RegularExpressions;
using System.Reflection;
using System.Threading;
using System.IO;
using System.Data;

namespace _videoConference
{	
    sealed public partial class LogOn : Page,IDisposable
	{
        int value;
        IAddPage ia = new IAddPage();
        System.Data.DataSet ds = new System.Data.DataSet();
        private bool disposed = false;

        public LogOn()
		{
			InitializeComponent();
            UserInfo.MyCurrentPage = "LogOn";
            this.ShowsNavigationUI = false;

            ConfigLink.Click += new RoutedEventHandler(ConfigLink_Click);                   
            Uri adiUri = new Uri("http://www.adiance.com/");
            adiancehlink.NavigateUri = adiUri;          
            lblRegiUser.Content = "Enter as a registered user";
            lblLogin.Content = "Login:";
            lblPassword.Content = "Password:";
            btnLogin.Content = "Login";
            btnGuest.Content = "Click To Enter";
            lblGuest.Content = "Enter as a guest";
            lblName.Content = "Enter your meeting E-Mail Id:";
            lblLoginGuest.Height = 53.2;            

            try
            {                
                if (!string.IsNullOrEmpty(UserInfo.UserName))
                {
                    txtGuestUser.Text = UserInfo.UserName;
                }
                if (UserInfo.MyRole == "Host")
                {
                    btnLogin.IsEnabled = true;
                    btnGuest.IsEnabled = false;
                }
                else
                {
                    btnGuest.IsEnabled = true;
                    btnLogin.IsEnabled = false;
                }

                if (UserInfo.strMeetingMode == "Meeting Mode")
                {
                    try
                    {
                        PeerServices.OpenChannels();
                    }
                    catch (Exception exp)
                    {
                        exp.Data.Add("My Key", "1videoConference--:--LogOn.xaml.cs--:--LogOn()--:--");
                        clsUtility.LogException = exp;
                        //MessageBox.Show("Peerservices" + exp.Message);
                    }
                }
            }
            catch (Exception exp)
            {
                exp.Data.Add("My Key", "1videoConference--:--LogOn.xaml.cs--:--LogOn()1--:--");
                clsUtility.LogException = exp;
            }
		}

        void ConfigLink_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                NavigationService.GetNavigationService(this).Navigate(new Uri("Config.xaml", UriKind.RelativeOrAbsolute));
                return;
            }
            catch (Exception exp)
            {
                exp.Data.Add("My Key", "1videoConference--:--LogOn.xaml.cs--:--ConfigLink_Click()--:--");
                clsUtility.LogException = exp;
            }
        }

        public void logolink(object o, MouseButtonEventArgs e)
        {            
            vclink.NavigateUri = new Uri("http://www.1videoconference.com", false);
            vclink.TargetName = "window1";
            vclink.DoClick();
        }

        void UserPass_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            try
            {
                if (UserPass.Password.Length > 0 && !string.IsNullOrEmpty(txtUserName.Text))
                {
                    if (e.Key == Key.Enter)
                    {
                        LogOnClick(new object(), new EventArgs());
                    }
                    btnLogin.IsEnabled = true;
                }
                else
                {
                    btnLogin.IsEnabled = false;
                }
            }
            catch (Exception exp)
            {
                exp.Data.Add("My Key", "1videoConference--:--LogOn.xaml.cs--:--UserPass_KeyDown()--:--");
                clsUtility.LogException = exp;
            }
        }

        void txtUserName_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            try
            {
                if (UserPass.Password.Length > 0 && !string.IsNullOrEmpty(txtUserName.Text))
                {
                    if (e.Key == Key.Enter)
                    {
                        LogOnClick(new object(), new EventArgs());
                    }
                    btnGuest.IsEnabled = true;
                }
                else
                {
                    btnGuest.IsEnabled = false;
                }
            }
            catch (Exception exp)
            {
                exp.Data.Add("My Key", "1videoConference--:--LogOn.xaml.cs--:--UserName_KeyDown()--:--");
                clsUtility.LogException = exp;
            }
        }

		private void Signup(object sender, EventArgs e)
        {
            try
            {
                NavigationService.GetNavigationService(this).Navigate(new Uri("EmailEntry.xaml", UriKind.RelativeOrAbsolute));
                return;
            }
            catch (Exception exp)
            {
                exp.Data.Add("My Key", "1videoConference--:--LogOn.xaml.cs--:--SignUp()--:--");
                clsUtility.LogException = exp;
            }
	    }

        void Guest(object sender, EventArgs e)
        {
            try
            {
                if (txtGuestUser.Text == "")
                {
                    System.Windows.MessageBox.Show("Enter Guest Name First");
                }
                else
                {
                    value = ia.guestmeetinginfo(txtGuestUser.Text,UserInfo.MeetingID);
                    if (value == 1)
                    {                                        
                        NavigationService.GetNavigationService(this).Navigate(new Uri("Loading.xaml", UriKind.RelativeOrAbsolute));
                        return;                      
                    }
                    else
                    {
                        System.Windows.MessageBox.Show("You are not authorized to join meeting at this time.", "LogError");
                    }
                }
            }
            catch (Exception exp)
            {
                exp.Data.Add("My Key", "1videoConference--:--LogOn.xaml.cs--:--Guest()--:--");
                clsUtility.LogException = exp;
            }
        }

        void LogOnClick(object sender, EventArgs e)
        {
            try
            {
                if (UserInfo.strMeetingMode == "Meeting Mode")
                {
                    value = ia.meetinginfo(txtUserName.Text, UserPass.Password.ToString(), UserInfo.MeetingID);

                    if (value == 1)
                    {
                        NavigationService.GetNavigationService(this).Navigate(new Uri("Loading.xaml", UriKind.RelativeOrAbsolute));
                        return;
                    }

                    else if (value == 0)
                    {
                        System.Windows.MessageBox.Show("You are not allow to start meeting at this time", "LogErr");
                    }
                }
                else if (UserInfo.strMeetingMode == "Design Mode")
                {
                    int result = ia.uservalidation(txtUserName.Text, UserPass.Password.ToString());

                    if (result == 1)
                    {
                        NavigationService.GetNavigationService(this).Navigate(new Uri("Loading.xaml", UriKind.RelativeOrAbsolute));
                        return;
                    }
                    else
                    {
                        System.Windows.MessageBox.Show("Enter Correct User Name and Password to Login", "LogMessage");
                    }
                }
            }
            catch (Exception exp)
            {
                //MessageBox.Show("Logon:" + exp.Message);
                exp.Data.Add("My Key", "1videoConference--:--LogOn.xaml.cs--:--LogOnClick()--:--");
                clsUtility.LogException = exp;
            }
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);    
        }

         private void Dispose(bool disposing)
        {
            if(!this.disposed)
            {
                if(disposing)
                { 
                   
                }        
                disposed = true;

            }
        }

        ~LogOn()
        {
            Dispose(false);
        }

	}
}